package de.homelabs.hlmodules.cms.dev;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import de.homelabs.hlmodules.cms.api.template.service.IHLTemplateImportStrategy;
import de.homelabs.hlmodules.cms.configuration.domain.HLConfigurationEntity;
import de.homelabs.hlmodules.cms.enums.HLErrorCode;
import de.homelabs.hlmodules.cms.template.domain.HLImportStrategyException;
import de.homelabs.hlmodules.cms.template.domain.HLTemplate;
import de.homelabs.hlmodules.cms.template.domain.HLTemplateException;

/**
 * generic class that reads a xml from classpath an converts its content to an
 * object structure<br />
 * <br />
 * 
 * @author D.Mueller
 * 
 * @param <T>
 *            - Class of the xml root element
 */
public class HLJAXBImportStrategy<T> implements IHLTemplateImportStrategy {

	// vars
	private Class<?>[] registeredClasses = null;

	/**
	 * default constructor
	 */
	public HLJAXBImportStrategy() {
	}

	/**
	 * parameter constructor
	 * 
	 * @param registeredClass
	 */
	public HLJAXBImportStrategy(Class<?>[] importClasses) {
		this.registeredClasses = importClasses;
	}

	/**
	 * imports an element list using jaxb strategy
	 * 
	 * @param String
	 *            fileName
	 * @return T
	 */
	public List<HLTemplate> importTemplateList(String fileName)
			throws HLImportStrategyException {
		// jaxbcontext
		// JAXBContext context = createJAXBContext();
		// Unmarshaller um;
		//
		// try {
		// //get JAXBContext
		// um = context.createUnmarshaller();
		// //read xml
		// InputStream in = this.getClass().getResourceAsStream(fileName);
		// //unmarshall
		// return (T) um.unmarshal(in);
		// } catch (JAXBException e){
		// //cast JAXBExceptions to HLImportStrategyException
		// throw new
		// HLImportStrategyException(HLErrorCode.UNDERLAYING_EXCEPTION,
		// e.getClass(), e.getLocalizedMessage());
		// }
		return null;
	}

	/****************
	 * functions *
	 ****************/
	/**
	 * create JAXB context and register classes
	 * 
	 * @return JAXBContext
	 * @throws JAXBException
	 */
	public JAXBContext createJAXBContext() throws HLImportStrategyException {
		try {
			// classes to be imported and parsed needs to be registered
			if (registeredClasses != null) {
				// lets create a JAXBContext
				return JAXBContext.newInstance(registeredClasses, null);
			} else {
				// so we are missing some classes, throw import exception
				throw new HLImportStrategyException(
						HLErrorCode.MISSING_REGISTERED_CLASSES);
			}
		} catch (JAXBException e) {
			// cast JAXBExceptions to HLImportStrategyException
			throw new HLImportStrategyException(
					HLErrorCode.UNDERLAYING_EXCEPTION, e.getClass(),
					e.getLocalizedMessage());
		}
	}

	public Class<?>[] getRegisteredClasses() {
		return registeredClasses;
	}

	public void setRegisteredClasses(Class<?>[] registeredClasses) {
		this.registeredClasses = registeredClasses;
	}

	public HLTemplate findTemplateSource(HLConfigurationEntity configEntity,
			String normalizedDomain, String name) throws HLTemplateException {
		// TODO Auto-generated method stub
		return null;
	}

	public long getLastModified(Object templateSource) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Reader getReader(Object templateSource, String encoding)
			throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	public void closeTemplateSource(Object templateSource) throws IOException {
		// TODO Auto-generated method stub

	}

	public long getLastModified(HLTemplate templateSource) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Reader getReader(HLTemplate templateSource, String encoding)
			throws HLTemplateException {
		// TODO Auto-generated method stub
		return null;
	}

	public void closeTemplateSource(HLTemplate templateSource)
			throws HLTemplateException {
		// TODO Auto-generated method stub

	}
}
